package com.mz.ozy.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RatingBar;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.mz.ozy.R;
import com.mz.ozy.adapter.EvauInfoAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.MyOrderInfoBean;
import com.mz.ozy.bean.MyOrderInfoBean1;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.EvaluatePresenter;
import com.mz.ozy.mvp.view.EvaluateContract;
import com.mz.ozy.utils.QiNiuManger;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 发表评价
 * 只有团体课和直播有发表评价
 */
public class EvaluateActivity extends BaseActivity<EvaluateContract.View, EvaluatePresenter> implements EvaluateContract.View {


    @BindView(R.id.rb_course)
    RatingBar mRbCourse;
    @BindView(R.id.rb_coach)
    RatingBar mRbCoach;
    @BindView(R.id.rb_course1)
    RatingBar rb_course1;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;


    private static final int REQUEST_CODE_CHOOSE = 23;
    private int type;   //0-门店课程评价  1-直播课程评价
    private String scid;  //门店课程id
    private EvauInfoAdapter mAdapter1;
    private List<MyOrderInfoBean.OrdermapBean.ProdcutListBean> data;
    private List<List<String>> mAdapterImgArr = new ArrayList<>();
    private int mPosition;
    private int overall;
    //    private List<Integer> pjNum = new ArrayList<>();
//    private List<String> content = new ArrayList<>();
    private Map<Integer, String> pjNum = new HashMap<>();
    private Map<Integer, String> content = new HashMap<>();

    private String id;
    private int beanType;

    public static void go(Context context, List<MyOrderInfoBean.OrdermapBean.ProdcutListBean> bean, String scid, int beanType) {
        Intent intent = new Intent(context, EvaluateActivity.class);
        intent.putExtra("bean", (Serializable) bean);
        intent.putExtra("id", scid);
        intent.putExtra("beanType", beanType);
        context.startActivity(intent);
    }
    public static void go1(Context context, List<MyOrderInfoBean1.OrdermapBean.ProdcutListBean> bean, String scid, int beanType) {
        Intent intent = new Intent(context, EvaluateActivity.class);
        intent.putExtra("bean", (Serializable) bean);
        intent.putExtra("id", scid);
        intent.putExtra("beanType", beanType);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate);
        ButterKnife.bind(this);
        initTitle();
        data = (List<MyOrderInfoBean.OrdermapBean.ProdcutListBean>) getIntent().getSerializableExtra("bean");
        id = getIntent().getStringExtra("id");
        beanType = getIntent().getIntExtra("beanType", 0);
        tvCenterTitle.setText("评价");
        initData();
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter1 = new EvauInfoAdapter(this, data);
        recyclerview.setAdapter(mAdapter1);
        recyclerview.setNestedScrollingEnabled(false);
        mAdapter1.setOnAddPicClickListener(new EvauInfoAdapter.onAddPicClickListener() {
            @Override
            public void onAddPicClick(int adapterPosition) {
                mPosition = adapterPosition;
            }

            @Override
            public void onRemovePicClick(int adapterPosition, int index) {
                // mAdapter1.removeAdapterImg(adapterPosition, index);
            }

            @Override
            public void setSelectedNum(int tag, int adapterPosition) {
                pjNum.put(adapterPosition, tag + "");
//                pjNum.add(num);
            }

            @Override
            public void setSelectedNum1(String text, int adapterPosition) {
                content.put(adapterPosition, text);
//                content.clear();
//                content.add(num);
            }
        });
    }

    private void initData() {
        for (int i = 0; i < data.size(); i++) {
            mAdapterImgArr.add(new ArrayList<>());
        }
    }


    @OnClick(R.id.bt_confirm)
    public void onViewClicked() {

        List<JSONObject> mapList = new ArrayList<>();
        for (int index = 0; index < data.size(); index++) {
            List<String> imgarr = mAdapterImgArr.get(index);
            if (pjNum.size() < data.size()) {
                ToastUtils.showToast("请选择评价");
                return;
            }
            String imgArr = getImgArr(imgarr);
            final MyOrderInfoBean.OrdermapBean.ProdcutListBean productMapBean = data.get(index);
            Map<String, String> map = new HashMap<>();
            String productid = "";
            if (productMapBean.getProduct_id() != 0) {
                productid = productMapBean.getProduct_id() + "";
            } else {
                productid = productMapBean.getId() + "";
            }
            map.put("productid", productid);
            map.put("img", imgArr);
            map.put("overall", pjNum.get(index) + "");
            if (content.size() == 0) {
                map.put("content", "");
            } else {
                map.put("content", content.get(index));
            }
            JSONObject json = new JSONObject(map);
            mapList.add(json);
        }
        int courseStars = (int) mRbCourse.getRating();
        int coachStars = (int) mRbCoach.getRating();
        int rb_course = (int) rb_course1.getRating();
        if (courseStars == 0) {
            ToastUtils.showToast("请选择描述相符");
            return;
        }
        if (coachStars == 0) {
            ToastUtils.showToast("请选择服务态度");
            return;
        }
        if (rb_course == 0) {
            ToastUtils.showToast("请选择物流服务");
            return;
        }
        mPresenter.onEvaluate1(this, id, courseStars + "", rb_course + "", coachStars + "", mapList.toString(), beanType);
    }

    public String getImgArr(List<String> imgarr) {
        String submitImg = "";
        if (imgarr.size() == 0) {
            submitImg = "";
        } else {
            for (String str : imgarr) {
                submitImg = submitImg + str + ",";
            }
            submitImg = submitImg.substring(0, submitImg.length() - 1);
        }
        return submitImg;
    }

    //用户选中图片后，拿到回掉结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureConfig.CHOOSE_REQUEST && resultCode == RESULT_OK) {
            List<LocalMedia> images = PictureSelector.obtainMultipleResult(data);
            List<String> uploadlist = Utils.convertToImageList(images);
            if (uploadlist != null && uploadlist.size() > 0) {
                for (String str : uploadlist) {
                    uploadAvatar(str);
                }
            }
        }
    }


    private void uploadAvatar(final String path) {
        final String qiniuToken = SPUtils.getInstance(this).getString(Constants.SPName.QINIU_TOKEN, "");
        QiNiuManger.getInstance().getUploadManager().put(path, QiNiuManger.getInstance().getKeyName(), qiniuToken, new UpCompletionHandler() {
            @Override
            public void complete(String key, ResponseInfo info, JSONObject response) {
                if (info.isOK()) {
//                    img.add(key);
//                    adapter.addNewItem(path);
                    final List<String> imgArr = mAdapterImgArr.get(mPosition);
                    imgArr.add(key);
                    mAdapter1.addAdapterImg(mPosition, path);
                }
            }
        }, null);
    }

    @Override
    public void onEvaluateSuccess() {

    }

    @Override
    public void up(JSONObject jsonObject) {

        ToastUtils.showToast("评价成功")
        ;
        finish();
    }

    @Override
    public void onInitSuccess(MyOrderInfoBean myOrderInfoBean) {

    }

    @Override
    public void onInitError(String msg) {

    }
}
